//
//  RHOriginalView.swift
//  微博新浪
//
//  Created by hezerong on 16/6/13.
//  Copyright © 2016年 Rocky. All rights reserved.
// timeLable.text = "10分钟前"//sourceLable.text = "来自湖南微博"

import UIKit

class RHOriginalView: UIView {

    //viewmodel
    var statusViewModel:RHStatusViewModel?{
    
        didSet{
//            pictureView.viewModel = statusViewModel
        
            //添加测试数据
//             avatarIcon.sd_setImageWithURL(NSURL(string:(statusViewModel?.statusModel.user?.profile_image_url)!), placeholderImage: UIImage(named: "avatar_default_big"))
//            
//
//            avatarIcon.image = UIImage(named: "avatar_default_big")?.createCircleImage(true, size: CGSizeMake(36, 36), color: UIColor.whiteColor())
            
//            avatarIcon.setImage(statusViewModel?.statusModel.user?.profile_image_url, placeHolder: "avatar_default_big")
            
            avatarIcon.setCircleImage(statusViewModel?.statusModel.user?.profile_image_url, placeHolder: "avatar_default_big", iscornered: true, size: CGSizeMake(36, 36), color: UIColor.whiteColor())
            screenName.text = statusViewModel?.statusModel.user?.screen_name
            levelIcon.image = statusViewModel?.levelIconImage  //UIImage(named: "common_icon_membership_level1")
            vipIcon.image = statusViewModel?.vipIconImage //UIImage(named: "avatar_vip")
            timeLable.text = statusViewModel?.timeTexst //statusModel.created_at
            
            sourceLable.text = statusViewModel?.sourceText
            
//            var testTring:String = ""
//            
//            let random = arc4random() % 20 + 1
//            
//            for _ in (1...random){
//                
//                testTring += "damn you"
//            }
            
            statusLabel.text = statusViewModel?.statusModel.text
 
            
            //判断是否有配图视图
            //如果没有配图视图，则将配图视图相对于文字的间距设为0
            //如果有配图视图，则将配图视图相对于文字的间距设为cellLayout.seprator
//            var pictureOffset:CGFloat = 0
//            
//            if let pic_urls = statusViewModel?.statusModel.pic_urls where pic_urls.count > 0 {
//                pictureOffset = cellLayout_constant.separator
//            }
//            
//            pictureView.snp_updateConstraints { (make) -> Void in
//                make.top.equalTo(statusLabel.snp_bottom).offset(pictureOffset)
//                make.left.equalTo(statusLabel)
//                make.size.equalTo((statusViewModel?.pictureViewSize)!)
//                make.bottom.equalTo(self).offset(-cellLayout_constant.separator)
//            }
            
        
        }
    }
    
    //头像
    let avatarIcon = UIImageView()
    //昵称
    let screenName = UILabel(text: "",  fontSize: 16)
    //皇冠图标
    let levelIcon = UIImageView()
    //vip图标
    let vipIcon = UIImageView()
    //发布时间
    let timeLable = UILabel(text: nil, textColor: UIColor(white: 0.6, alpha: 1.0), fontSize: 12)
    //来源
    let sourceLable = UILabel(text: nil, textColor: UIColor.lightGrayColor(), fontSize: 12)
    
    //微博文字
    let statusLabel = UILabel(text: nil,  fontSize: 15)
    
//    //图片视图
//    let pictureView:RHPictureView = RHPictureView()

    
    
    override init(frame: CGRect) {
        super.init(frame: CGRectZero)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

extension RHOriginalView{

    func setupUI(){
    
       //添加子控件
        addSubview(avatarIcon)
        addSubview(screenName)
        addSubview(levelIcon)
        addSubview(vipIcon)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(statusLabel)
//        addSubview(pictureView)
        
//       pictureView.backgroundColor = UIColor.yellowColor()
        
        
        //自动布局
        avatarIcon.snp_makeConstraints { (make) -> Void in
            make.size.equalTo(CGSizeMake(36, 36))
            make.left.equalTo(self).offset(12)
            make.top.equalTo(self).offset(12)
        }
        
        screenName.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(avatarIcon.snp_right).offset(12)
            make.top.equalTo(avatarIcon)        }
        
        levelIcon.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(screenName.snp_right).offset(3)
            make.top.equalTo(screenName).offset(3)
        }
        
        vipIcon.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(avatarIcon.snp_right).offset(-10)
            make.bottom.equalTo(avatarIcon).offset(5)
        }
        
        timeLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(avatarIcon.snp_right).offset(12)
            make.bottom.equalTo(avatarIcon)

        }
        sourceLable.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLable.snp_right).offset(12)
            make.bottom.equalTo(avatarIcon)
        }
        
        
        //测试微博正文
        statusLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(avatarIcon)
            make.right.equalTo(self).offset(-12)
            make.top.equalTo(avatarIcon.snp_bottom).offset(12)
            make.bottom.equalTo(self)
        }
        
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(statusLabel.snp_bottom).offset(12)
//            make.left.equalTo(statusLabel)
//            make.size.equalTo(CGSizeZero)
//            make.bottom.equalTo(self).offset(-cellLayout_constant.separator)
//            make.height.equalTo(200)
//        }
        
    }
}